package com.zxy.ziems.server.device.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.alarm.pojo.param.DeviceAlarmPageParam;
import com.zxy.ziems.server.alarm.pojo.vo.AlarmRecordVO;
import com.zxy.ziems.server.alarm.service.AlarmRecordService;
import com.zxy.ziems.server.energy.pojo.param.DeviceMeasureDataPageParam;
import com.zxy.ziems.server.energy.pojo.param.DeviceMeasureDataQParam;
import com.zxy.ziems.server.energy.pojo.vo.DeviceMeasureDataVO;
import com.zxy.ziems.server.energy.service.DeviceMeasureDataService;
import com.zxy.ziems.server.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 采集管理-设备采集相关的查询
 *
 * @author 10348359
 * @create 2024-04-03 13:23
 */
@RestController
@Validated
@RequestMapping("/api")
public class DeviceCollectorViewController {

    /**
     * 分页查询采集数据
     *
     * @param param 分页参数
     * @return
     */
    @GetMapping("/device/collector/measure/record/page")
    public PageResult<DeviceMeasureDataVO> pageByDevice(DeviceMeasureDataPageParam param) throws Exception {
        if (Objects.isNull(param.getStartTime()) && Objects.isNull(param.getEndTime())) {
            LocalDateTime now = LocalDateTimeUtil.now();
            LocalDateTime end = LocalDateTimeUtil.endOfDay(now);
            // 查询7天内数据
            LocalDateTime start = end.minusDays(7);
            param.setStartTime(start);
            param.setEndTime(end);
        }
        param.setStartTime(DateUtils.getStartTime(param.getStartTime().toLocalDate()));
        param.setEndTime(DateUtils.getEndTime(param.getEndTime().toLocalDate()));
        return measureDataService.pageByDevice(param);
    }


    /**
     * 数据管理-》分页查询
     *
     * @param param 分页参数
     * @return
     */
    @GetMapping("/device/collector/measure/data/page")
    public PageResult<DeviceMeasureDataVO> measureDataPageList(DeviceMeasureDataQParam param) throws Exception {
//        if (Objects.isNull(param.getStartTime()) && Objects.isNull(param.getEndTime())) {
//            LocalDateTime now = LocalDateTimeUtil.now();
//            LocalDateTime end = LocalDateTimeUtil.endOfDay(now);
//            LocalDateTime start = end.minusDays(30);
//            param.setStartTime(start);
//            param.setEndTime(end);
//        }
        return measureDataService.measureDataPageList(param);
    }


    /**
     * 分页查询告警数据
     *
     * @param param 分页参数
     * @return
     */
    @GetMapping("/device/collector/alarm/record/page")
    public PageResult<AlarmRecordVO> alarmPageByDevice(DeviceAlarmPageParam param) throws Exception {
        if (Objects.isNull(param.getAlarmStartTime()) && Objects.isNull(param.getAlarmEndTime())) {
            LocalDateTime now = LocalDateTimeUtil.now();
            LocalDateTime end = LocalDateTimeUtil.endOfDay(now);
            LocalDateTime start = end.minusDays(30);
            param.setAlarmStartTime(start);
            param.setAlarmEndTime(end);
        }
        return alarmRecordService.pageByDevice(param);
    }

    private DeviceMeasureDataService measureDataService;

    @Autowired
    public void setMeasureDataService(DeviceMeasureDataService deviceMeasureDataService) {
        this.measureDataService = deviceMeasureDataService;
    }

    private AlarmRecordService alarmRecordService;

    @Autowired
    public void setAlarmRecordService(AlarmRecordService alarmRecordService) {
        this.alarmRecordService = alarmRecordService;
    }
}
